﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace CRM_2013
{
    public partial class membersCtr : DevExpress.XtraEditors.XtraUserControl
    {

        Stack<Guid> list;
        Guid sourceProduct;
        
        public membersCtr()
        {
            InitializeComponent();
            list = new Stack<Guid>();
            sourceProduct = Guid.Empty;

        }

        private void membersCtr_Load(object sender, EventArgs e)
        {
            lessonsTA.FillMain(dataSet1.Lessons);
            membersTA.FillAll(dataSet1.Members);
            paymentsTA.FillAll(dataSet1.Payments);
            creditsTA.Fill(dataSet1.Credits);
            contactsTA.Fill(dataSet1.Contacts);
            typePaymentTA.Fill(dataSet1.TypePayment);
            
        }

        public void updateDataMembers()
        {
            paymentsTA.Update(dataSet1.Payments);
            creditsTA.Update(dataSet1.Credits);
            membersTA.Update(dataSet1.Members);
            
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (radioGroup1.SelectedIndex)
            {
                case 0:
                    bsLessons.Filter = "IsPast =0";
                    break;
                case 1:
                    bsLessons.Filter = "";
                    break;
            }
        }

        private void gridViewMembers_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            GridView View = sender as GridView;
            string dolg="";

            if (e.RowHandle >= 0)
            {
                try
                {
                    dolg = View.GetRowCellDisplayText(e.RowHandle, View.Columns["colDolg"]);
                                      
                }
                catch
                {
                    dolg = "";
                }
                if (dolg.Equals("") || !dolg.Equals("0,00"))
                {
                    e.Appearance.BackColor = Color.Salmon;
                    e.Appearance.BackColor2 = Color.SeaShell;
                }
            }
        }

        private void gridViewMembers_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView memberView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            DevExpress.XtraGrid.Views.Grid.GridView lessonsView = gridViewLessons as DevExpress.XtraGrid.Views.Grid.GridView;
            int rowHandle = e.RowHandle;
            int lessonAmount;

            Guid lessonId = (Guid)lessonsView.GetRowCellValue(lessonsView.FocusedRowHandle, lessonsView.Columns["idLessons"]);
            try
            {
                lessonAmount = (int)lessonsView.GetRowCellValue(lessonsView.FocusedRowHandle, lessonsView.Columns["Amount"]);
            }
            catch
            {
                lessonAmount = 0;
            }

            memberView.SetRowCellValue(rowHandle, memberView.Columns["idMembers"], Guid.NewGuid());
            memberView.SetRowCellValue(rowHandle, memberView.Columns["ref_Lessons"], lessonId);
            memberView.SetRowCellValue(rowHandle, memberView.Columns["Amount"], lessonAmount);
            memberView.SetRowCellValue(rowHandle, memberView.Columns["IsTakePart"], 0);
            memberView.SetRowCellValue(rowHandle, memberView.Columns["IsPaid"], 0);
            memberView.SetRowCellValue(rowHandle, memberView.Columns["IsInterview"], 0);
        }

        private void repItemLookUpContact_EditValueChanged(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView member = gridViewMembers as DevExpress.XtraGrid.Views.Grid.GridView;
            
            DevExpress.XtraEditors.LookUpEdit edit = sender as DevExpress.XtraEditors.LookUpEdit;
            string phone = edit.GetColumnValue(repItemLookUpContact.Columns[1]).ToString();


            member.SetRowCellValue(member.FocusedRowHandle, member.Columns["ContactPhone"], phone);
            
            
        }

        private void gridViewPayments_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView childView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            DevExpress.XtraGrid.Views.Grid.GridView parentView = childView.ParentView as DevExpress.XtraGrid.Views.Grid.GridView;
            int rowHandle = e.RowHandle;

            Guid parentId = (Guid)parentView.GetRowCellValue(childView.SourceRowHandle, parentView.Columns["idMembers"]);
            Guid contactId = (Guid)parentView.GetRowCellValue(childView.SourceRowHandle, parentView.Columns["ref_Contacts"]);

            childView.SetRowCellValue(rowHandle, childView.Columns["idPayments"], Guid.NewGuid());
            childView.SetRowCellValue(rowHandle, childView.Columns["ref_Members"], parentId);
            childView.SetRowCellValue(rowHandle, childView.Columns["ref_Contacts"], contactId);
            childView.SetRowCellValue(rowHandle, childView.Columns["Date"], DateTime.Now);
        }

        private void gridViewPayments_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            DevExpress.XtraGrid.Views.Grid.GridView parentView = view.ParentView as DevExpress.XtraGrid.Views.Grid.GridView;
            int oplata;

            DevExpress.XtraGrid.Columns.GridColumn col = view.Columns["Sum"];
            DevExpress.XtraGrid.GridSummaryItem summary = col.SummaryItem;

            try
            {
                oplata = Convert.ToInt32(summary.SummaryValue);
            }
            catch
            {
                oplata = 0;
            }

            parentView.SetRowCellValue(parentView.FocusedRowHandle, parentView.Columns["Oplata"], oplata);


        }

        private void gridViewCredits_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView childView = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            DevExpress.XtraGrid.Views.Grid.GridView parentView = childView.ParentView as DevExpress.XtraGrid.Views.Grid.GridView;
            int rowHandle = e.RowHandle;

            Guid parentId = (Guid)parentView.GetRowCellValue(childView.SourceRowHandle, parentView.Columns["idMembers"]);
            Guid contactId = (Guid)parentView.GetRowCellValue(childView.SourceRowHandle, parentView.Columns["ref_Contacts"]);

            childView.SetRowCellValue(rowHandle, childView.Columns["idCredits"], Guid.NewGuid());
            childView.SetRowCellValue(rowHandle, childView.Columns["ref_Members"], parentId);
            childView.SetRowCellValue(rowHandle, childView.Columns["ref_Contacts"], contactId);
            childView.SetRowCellValue(rowHandle, childView.Columns["Date"], DateTime.Now);
        }

        private void radioGroup2_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (radioGroup2.SelectedIndex)
            {
                case 0:
                    bsMembers.Filter = "";
                    break;
                case 1:
                    bsMembers.Filter = "IsPaid=1";
                    break;
                case 2:
                    bsMembers.Filter = "IsTakePart=1";
                    break;
            }
        }

        private void btnAddMembers_Click(object sender, EventArgs e)
        {
            membersAddContactsGroup formAdd = new membersAddContactsGroup();

            DevExpress.XtraGrid.Views.Grid.GridView lessonsView = gridViewLessons as DevExpress.XtraGrid.Views.Grid.GridView;
            int rowHandle = lessonsView.FocusedRowHandle;
            int lessonAmount;

            Guid lessonId = (Guid)lessonsView.GetRowCellValue(rowHandle, lessonsView.Columns["idLessons"]);
            try
            {
                lessonAmount = (int)lessonsView.GetRowCellValue(rowHandle, lessonsView.Columns["Amount"]);
            }
            catch
            {
                lessonAmount = 0;
            }

            formAdd.refLesson = lessonId;
            formAdd.amount = lessonAmount;

            if (formAdd.ShowDialog() == DialogResult.OK)
            {
                membersTA.FillAll(dataSet1.Members);
            }

            formAdd.Dispose();
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            int[] rows= gridViewMembers.GetSelectedRows();

            list.Clear();

            for (int i = 0; i < rows.GetLength(0); i++)
            {
                list.Push((Guid)gridViewMembers.GetRowCellValue(rows[i], gridViewMembers.Columns["idMembers"]));
            }

            try
            {
                sourceProduct = (Guid)gridViewLessons.GetRowCellValue(gridViewLessons.FocusedRowHandle, gridViewLessons.Columns["ref_Product"]);
            }
            catch
            {
                sourceProduct = Guid.Empty;
            }

            btnPaste.Enabled = true;

        }

        private void btnPaste_Click(object sender, EventArgs e)
        {
            int pasteWithPayments = 0;
            Guid destProduct;

            try
            {
                destProduct = (Guid)gridViewLessons.GetRowCellValue(gridViewLessons.FocusedRowHandle, gridViewLessons.Columns["ref_Product"]);
            }
            catch
            {
                destProduct = Guid.Empty;
            }

            if (sourceProduct.Equals(Guid.Empty) || destProduct.Equals(Guid.Empty) || !sourceProduct.Equals(destProduct))
            {
                MessageBox.Show("Типы исходного и конечного тренингов не совпадают или неопределенны. Будут скопированы только данные об участниках.", "Внимание!");
                pasteWithPayments = 0;
            }
            else
            {
                if (MessageBox.Show("Есть возможность перенести данные о платежах участников в конечный тренинг. Перенести данные о платежах?", "Внимание!", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    pasteWithPayments = 1;
                }
                else
                {
                    pasteWithPayments = 0;
                }
            }

            Guid destLesson = (Guid)gridViewLessons.GetRowCellValue(gridViewLessons.FocusedRowHandle, gridViewLessons.Columns["idLessons"]);

            updateDataMembers();
            
            
            foreach (Guid oldMembers in list)
            {
                membersTA.PasteMembers(pasteWithPayments, oldMembers, destLesson);
            }

            membersTA.FillAll(dataSet1.Members);
            paymentsTA.FillAll(dataSet1.Payments);

            list.Clear();
            btnPaste.Enabled = false;

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            gridControl2.ShowPrintPreview();
        }
      

    }
}
